const { Controller } = require("egg");
const path = require("path");
const fs = require("fs");
const { respAdapter } = require("../../utils");

class UserController extends Controller {
  async login() {
    const { ctx, app } = this;
    const body = this.ctx.request.body;
    console.log("😈body", body);
    try {
      const user = await this.app.mysql.get("users", body);

      const { user_name, password } = body;

      const token = app.jwt.sign(
        {
          UserName: user_name,
          Password: password,
        },
        app.config.jwt.secret,
        { expiresIn: "3d" }
      );
      if (!user) {
        ctx.body = respAdapter(null, "用户账号密码错误，请重试", false);
      } else {
        if (user?.state != 1) {
          ctx.body = respAdapter(null, "该用户已被禁用", false);
          return;
        }

        ctx.body = respAdapter({ token, ...user }, "");
      }
    } catch {
      ctx.body = respAdapter(null, "登录失败，请检查", false);
    }
  }

  async verify() {
    const { ctx, app, config } = this;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      const user = app.jwt.verify(token, config.jwt.secret);
      console.log("😈user", user);
      ctx.body = respAdapter(null, "token验证成功");
    } catch (error) {
      ctx.body = respAdapter(null, "token验证失败", false);
    }
  }

  async list() {
    const { ctx, app, config } = this;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const list = await this.app.mysql.select("users", {});

      if (!list) {
        ctx.body = respAdapter(null, "数据异常，请重试", false);
      }

      ctx.body = respAdapter({ list });
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async create() {
    const { ctx, app, config } = this;
    const { body } = ctx.request;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const resp = await this.app.mysql.insert("users", body);
      ctx.body = respAdapter({}, null, !!resp);
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async update() {
    const { ctx, app, config } = this;
    const { body } = ctx.request;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const resp = await this.app.mysql.update("users", body);
      ctx.body = respAdapter({}, null, !!resp);
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async delete() {
    const { ctx, app, config } = this;
    const { body } = ctx.request;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const resp = await this.app.mysql.delete("users", body);
      ctx.body = respAdapter({}, null, !!resp);
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }
}

module.exports = UserController;
